Exercises

Please complete each exercise or answer the question before reviewing the posted solution comments.

Use the clear command before starting your work on a new problem to ensure that any existing variable name assignments don't interfere with your work on the new problem.

  1. Write a while loop to display N asterisks (*), one on each line, where N is a number given at the beginning of your script. For example, if N is 5, your script should print out:

    *
    *
    *
    *
    *

    This while loop will display N asterisks on N different lines.

    n = 1;
    while n <= N
       disp('*');
       n = n + 1;
    end
  2. Write a while loop to display N asterisks (*) on one line. Assume that N has already been assigned a number. If N is greater than 80, display only 80 asterisks. If N is less than 1, display no asterisks.

    For example, if N is 5, your script should print out

    *****

    Here is a fragment that will work if N has previously had its value assigned. Notice how the variable starline with N asterisks is created in the loop and it is printed after the loop ends. This is because the disp() command prints a new line after its argument.

    if N > 80
       N = 80;
    end
    n = 1;
    starline = '';
    while n <= N
       starline = [starline '*'];
       n = n + 1;
    end
    disp(starline);
  3. Write a code fragment that uses a while loop to repeatedly prompt the user for positive integer or zero. As long as negative values or non-integers are entered, your loop should ask the user for a positive integer.

    Here is a script that will work.

    val = -1;
    while val < 0 | rem(val,1) ~= 0
        val = input('Enter a positive integer or zero: ');
    end
    
  4. Write a while loop that counts the number of values in a vector that are equal to some number N and displays that value. Assume that the vector is saved as values and that N has already been assigned.

    This fragment assumes that N and values have already been assigned. This could be the case if the fragment was in the body of a function.

    k = 1;                % index of first value to check
    K = length(values);   % index of last value to check
    count = 0;            % initialize a counter variable
    
    while k <= K
       if values(k) == N
           count = count + 1;
       end
       k = k + 1;
    end
    
    disp(['There are ', num2str(count), ' values of ', num2str(N),' in the vector ']);
    
  5. Write a script that asks the user for a value, displays if the value is prime or not. The program should continue until the user types zero.

    This code fragment does not require that any values have been previously assigned. Save it as CheckPrime.m and try it.

    value = input('Enter a value or 0 to exit: '); % initialize the loop variable
    while value ~= 0
        if isprime(value)
            disp([num2str(value) ' is prime']);
        else
            disp([num2str(value) ' is NOT prime']);
        end
        value = input('Enter a value or 0 to exit: '); % get next value
    end